﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IService.Business;
using IService.Utility;
using IService.Model;

namespace IService.User_center
{
    public partial class ApplyActivity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindData();
            }
        }

        public void BindData()
        {
            //邦定活动列表
            DataTable table = DTVBusiness.ActivityManager.GetActivityListAll();
            this.ddlActivity.DataTextField = "Title";
            this.ddlActivity.DataValueField = "ID";
            this.ddlActivity.DataSource = table;
            this.ddlActivity.DataBind();
            this.ddlActivity.SelectedValue = ActivityID;

            //邦定个人信息
            this.txtRealUserName.Text = SessionState.CurrentUser.RealUserName;
            this.txtCompanyName.Text = SessionState.CurrentUser.CompanyName;
            this.txtPhone.Text = SessionState.CurrentUser.Phone;
            this.txtFixedPhone.Text = SessionState.CurrentUser.CompanyPhone;
            this.txtEmail.Text = SessionState.CurrentUser.Email;
            this.txtAddress.Text = SessionState.CurrentUser.CompanyAddress;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            /*检测各字段非空*/
            string applyName = this.txtRealUserName.Text.Trim();
            string applyCompanyName = this.txtCompanyName.Text.Trim();
            string applyPhone = this.txtPhone.Text.Trim();
            string applyEmail = this.txtEmail.Text.Trim();
            string applyAddress = this.txtAddress.Text.Trim();
            //检查信息是否完整
            if (string.IsNullOrEmpty(applyName))
            {
                this.lblErrorSupply.Text = "<font color='red'>请填写申请人姓名！</font>";
            }
            else if (string.IsNullOrEmpty(applyCompanyName))
            {
                this.lblErrorSupply.Text = "<font color='red'>请填写公司名称！</font>";
            }
            else if (string.IsNullOrEmpty(applyPhone))
            {
                this.lblErrorSupply.Text = "<font color='red'>清填写联系电话！</font>";
            }
            else if (string.IsNullOrEmpty(applyEmail))
            {
                this.lblErrorSupply.Text = "<font color='red'>清填写邮箱地址！</font>";
            }
            else if (string.IsNullOrEmpty(applyAddress))
            {
                this.lblErrorSupply.Text = "<font color='red'>清填写联系地址！</font>";
            }
            else
            {
                ActivityApply apply = new ActivityApply();
                apply.UserId = SessionState.CurrentUser.ID;
                apply.ActivityID = Convert.ToInt64(this.ddlActivity.SelectedValue);
                apply.RealUserName = this.txtRealUserName.Text;
                apply.CompanyName = this.txtCompanyName.Text;
                apply.Phone = this.txtPhone.Text;
                apply.FixedPhone = this.txtFixedPhone.Text;
                apply.Email = this.txtEmail.Text;
                apply.Address = this.txtAddress.Text;
                apply.ApplyDate = DateTime.Now;
                apply.UpdateDateTime = DateTime.Now;
                bool hassuccess = apply.Save();

                //多人报名情况
                if (!string.IsNullOrEmpty(txtRealUserName1.Text))
                { 
                    apply.ID=0;
                    apply.RealUserName=txtRealUserName1.Text;
                    hassuccess = hassuccess && apply.Save();
                }

                if (!string.IsNullOrEmpty(txtRealUserName2.Text))
                {
                    apply.ID = 0;
                    apply.RealUserName = txtRealUserName2.Text;
                    hassuccess = hassuccess && apply.Save();
                }

                if (!string.IsNullOrEmpty(txtRealUserName3.Text))
                {
                    apply.ID = 0;
                    apply.RealUserName = txtRealUserName3.Text;
                    hassuccess = hassuccess && apply.Save();
                }

                if (!string.IsNullOrEmpty(txtRealUserName4.Text))
                {
                    apply.ID = 0;
                    apply.RealUserName = txtRealUserName4.Text;
                    hassuccess = hassuccess && apply.Save();
                }

                //最终是否报名成功
                if (hassuccess)
                {
                    //JsUtil.AlertConfirm("保存成功！", this.Page);        
                    JsUtil.AlertAndRedirect("活动报名成功！", "ActivityList.aspx", this.Page);
                }
                else
                {
                    JsUtil.Alert("活动报名失败,请重新操作！", this.Page);
                }

            }
        }

        protected string ActivityID
        {
            get
            {
                try
                {
                    return base.Request["activityId"];
                }
                catch
                {
                    return "0";
                }
            }
        }
    }
}